define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/index' + location.search,
                    add_url: 'exam/add',
                    edit_url: 'exam/edit',
                    del_url: 'exam/del',
                    multi_url: 'exam/multi',
                    import_url: 'exam/import',
                    table: 'exam',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'intro', title: __('Intro'), operate: 'LIKE'},
                        {field: 'needtime', title: __('Needtime')},
                        {field: 'total_point', title: __('Total_point')},
                        {field: 'pass_point', title: __('Pass_point')},
                        {field: 'xuefen', title: __('Xuefen')},
                        {field: 'tifen', title: __('Tifen')},
                        // {field: 'avideo_id', title: __('Avideo_id')},
                        // {field: 'cate_id', title: __('Cate_id')},
                        {field: 'avideo.name', title: __('Avideo.name'), operate: 'LIKE'},
                        {field: 'period.name', title: "考试所属学习阶段", operate: 'LIKE'},
                        // {field: 'category.name', title: __('Category.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '查看试题',
                                    title: '查看试题',
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-address-book-o',
                                    url:function(row){
                                        return 'exam_item/index?ids='+row.id
                                    },
                                }
                            ],
                            events: Table.api.events.operate, formatter: function(value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if ((row.group_id===9&&row.avideo.status>2)||row.group_id===7||row.group_id===6) {
                                    $(table).data("operate-del", null);
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,  formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'operate', title: '添加试题', table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if ((row.group_id===9&&row.avideo.status>2)||row.group_id===7||row.group_id===6){
                                    return '';
                                }else{
                                    return '<a href="exam_item/add?ids='+ row["id"] + '" class="btn btn-xs btn-warning btn-dialog" title="添加试题" > <i class="fa fa-users"></i> </a>';
                                }
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});